package com.lzb.newloan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lzb.newloan.R;
import com.lzb.newloan.pojo.PersonPojo;
import com.lzb.newloan.utils.AlertDialog;

import java.util.ArrayList;

public class OverduePhoneListAdapter extends BaseAdapter {

	private ArrayList<PersonPojo> pojo;
	private LayoutInflater mInflater;
	private Context context;
	private int flag;

	public OverduePhoneListAdapter(Context context, ArrayList<PersonPojo> pojo,
			int flag) {
		this.pojo = pojo;
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
		this.flag = flag;
	}

	public ArrayList<PersonPojo> getArrayList() {
		return pojo;
	}

	public void setArrayList(ArrayList<PersonPojo> arrayList) {
		this.pojo = arrayList;
	}

	@Override
	public int getCount() {
		if (pojo == null) {
			return 0;
		} else {
			if (pojo.size() > 1) {
				if (pojo.get(1).getDay() > 3 && pojo.get(1).getDay() < 5) {
					return 2;
				} else if (pojo.get(1).getDay() > 5 && pojo.get(1).getDay() < 8) {
					return 4;
				} else if (pojo.get(1).getDay() > 8
						&& pojo.get(1).getDay() < 10) {
					return 6;
				} else if (pojo.get(1).getDay() > 10
						&& pojo.get(1).getDay() < 12) {
					return 8;
				} else {
					return pojo.size();
				}
			}
			return pojo.size();
		}
	}

	@Override
	public Object getItem(int arg0) {

		return pojo.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int postition, View convertView, ViewGroup arg2) {
		ViewHoler viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHoler();
			convertView = mInflater.inflate(R.layout.adapter_overdue_phone,
					null);
			viewHolder.send_msg1 = (TextView) convertView
					.findViewById(R.id.send_msg1);
			viewHolder.call1 = (TextView) convertView.findViewById(R.id.call1);
			viewHolder.name_phone1 = (TextView) convertView
					.findViewById(R.id.name_phone1);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHoler) convertView.getTag();
		}
		final PersonPojo data = pojo.get(postition);
		viewHolder.name_phone1.setText(data.getCardusername());

		viewHolder.call1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
						.parse("tel:" + data.getPhone())));
			}
		});
		viewHolder.send_msg1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog dialog = new AlertDialog(context);
				dialog.builder();
				String name = data.getJkName();
				final String msg = "【钱宝包】您好，您的亲友" + name + "（"
						+ data.getJkPhone() + "）申请的" + data.getHk_money()
						+ "元借款已逾期" + data.getDay() + "天。" + name
						+ "申请借款时授权我们将您作为可协助联系的联系人，" + "烦请您转告" + name
						+ "尽快还款，以免再次打扰，谢谢！" + data.getPhone();
				dialog.setMsg(msg);
				dialog.setNegativeButton("暂不发送", new OnClickListener() {
					@Override
					public void onClick(View v) {

					}
				});
				dialog.setPositiveButton("立即发送", new OnClickListener() {

					@Override
					public void onClick(View v) {
						Uri smsToUri = Uri.parse("smsto:" + data.getPhone());

						Intent intent = new Intent(Intent.ACTION_SENDTO,
								smsToUri);

						intent.putExtra("sms_body", msg.split("!")[0]);

						context.startActivity(intent);
					}
				});
				dialog.show();
			}
		});
		return convertView;
	}

	public class ViewHoler {
		public TextView name_phone1, send_msg1, call1;
	}

}
